home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 10.8 KB | 291 lines | [TEXT/MPS ] |
- // UDynamicArray.h
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
-
- #ifndef __UDYNAMICARRAY__
- #define __UDYNAMICARRAY__
-
- // MacApp
-
- #ifndef __UOBJECT__
- #include "UObject.h"
- #endif
-
- // ANSI
-
- #ifndef __STDDEF__
- #include <stddef.h>
- #endif
-
-
- //----------------------------------------------------------------------------------------
- // Forward and external class declarations
- //----------------------------------------------------------------------------------------
-
- class CArrayIterator;
-
-
- const short kAllocationIncrement = 6; // Initial Allocation increment. Six
- // elements of slop shouldn't break
- // anybody and provides a _nice_ cushion
- // from the memory manager.
-
-
- //----------------------------------------------------------------------------------------
- // Constants for TDynamicArray::CompareElements and TSortedxxx::Compare
- //----------------------------------------------------------------------------------------
- enum {
- kItem1LessThanItem2 = -1,
- kItem1EqualItem2 = 0,
- kItem1GreaterThanItem2 = 1
- };
-
-
- //----------------------------------------------------------------------------------------
- // Constants for TSortedList::Search. The criteria is considered to be item 2
- //----------------------------------------------------------------------------------------
-
- enum {
- kItemGreaterThanCriteria = kItem1LessThanItem2,
- kItemEqualCriteria = kItem1EqualItem2,
- kItemLessThanCriteria = kItem1GreaterThanItem2
- };
-
- typedef short CompareResult;
- // Negative, zero, and positive results are all meaningful even though we have the
- // nice constants above.
-
-
- //----------------------------------------------------------------------------------------
- // procedure typedefs for parameters
- //----------------------------------------------------------------------------------------
-
- typedef CompareResult (*CompareIndexType)(ArrayIndex anItem, void* yourDataPtr);
-
- typedef CompareResult(* CompareElementsType)(void* element1,
- void* Element2,
- void* yourDataPtr);
-
-
- //----------------------------------------------------------------------------------------
- // TDynamicArray
- //----------------------------------------------------------------------------------------
-
- class TDynamicArray : public TObject
- {
- MA_DECLARE_CLASS;
-
- public:
- //------------------------------------------------------------------------------------
- // Creation/ Destruction Methods
- //------------------------------------------------------------------------------------
-
- TDynamicArray();
- // Constructor
- virtual ~TDynamicArray();
- // Destructor
-
- void IDynamicArray(ArrayIndex initialSize,
- short elementSize);
- // Initialize a new array with initialSize elements, Always call it once before
- // calling any other method. Never call it twice.
-
- virtual TObject* Clone();
-
- virtual void Free();
- // If enumeration of the array is in process, delete all the array elements, mark
- // the fFreeRequested flag for testing at completion of the enumeration and
- // return. Otherwise really free the array. Gee, wouldn't automatic storage
- // management be great!
-
- //------------------------------------------------------------------------------------
- // Stream I/O protocol support.
- //------------------------------------------------------------------------------------
-
- virtual void ReadFrom(TStream* aStream);
-
- virtual void WriteTo(TStream* aStream);
-
-
- //------------------------------------------------------------------------------------
- // Array manipulation primitives
- //------------------------------------------------------------------------------------
-
- void* operator[](ArrayIndex index) const;
- // Returns a pointer to the index-th element in the array.
- // the index is "zero" based.
-
- void* ComputeAddress(ArrayIndex index) const;
- // Returns a pointer to of the index-th element in the array.
- // the index is "one" based.
-
- ArrayIndex GetSize() const
- { return fSize; }
- // Returns the ACTUAL number of elements in the array.
-
- void SetArraySize(ArrayIndex theSize);
- // Sets the array allocation to handle up to theSize elements
-
- void InsertElementsBefore(ArrayIndex index,
- const void* ElementPtr,
- ArrayIndex count);
- // Insert Elements before the indicated Element. The index of the new element will
- // be index. If index = 1 this inserts at the start of the array. If index = fSize
- // + 1 this inserts at the end of the array. Signals Failure if unable to change
- // the size of the array. !!! NOTE MULTIPLE ( >1 ) element moves for non-power of
- // 2 element sizes are NOT yet supported!
-
- void ReplaceElementsAt(ArrayIndex index,
- const void* ElementPtr,
- ArrayIndex count);
- // Replaces the Elements at the indicated index. !!! NOTE MULTIPLE ( >1 ) element
- // moves for non-power of 2 element sizes are NOT yet supported!
-
- void DeleteElementsAt(ArrayIndex index,
- ArrayIndex count);
- // Deletes the Element at the indicated index. Compresses the array !!! NOTE
- // MULTIPLE ( >1 ) element moves for non-power of 2 element sizes are NOT yet
- // supported!
-
- void GetElementsAt(ArrayIndex index,
- void* ElementPtr,
- ArrayIndex count);
- // copies count elements to the location specified by ptr. !!! NOTE MULTIPLE ( >1
- // ) element moves for non-power of 2 element sizes are NOT yet supported!
-
- void DeleteAll();
- // Delete every element from the list Leave fSize at 0.
-
- //------------------------------------------------------------------------------------
- // Misc. functions
- //------------------------------------------------------------------------------------
-
- inline Boolean IsEmpty() const
- { return fSize == kEmptyIndex; }
- // Test if this array is empty or not.
-
- void Merge(TDynamicArray* aDynamicArray);
- // merges aDynamicArray with itself. leaves aDynamicArray unchanged. !!! NOTE
- // MULTIPLE ( >1 ) element moves for non-power of 2 element sizes are NOT yet
- // supported!
-
- //------------------------------------------------------------------------------------
- // Public Sorting access
- //------------------------------------------------------------------------------------
- virtual CompareResult CompareElements(void* Element1, void* Element2);
- // Returns the sort order between elements of the array. Default considers
- // all elements equivalent.
-
- virtual void Sort();
- // Sorts the list usingthe Compare and SortBy methods.
-
- virtual void SortElementsBy(CompareElementsType CompareItems, void* yourDataPtr);
- // Sorts the list using the supplied CompareItems function.
-
- //------------------------------------------------------------------------------------
- // Sorting
- //------------------------------------------------------------------------------------
- protected:
- void* GetSwapBuffer();
- // Returns a buffer to use in element exchanges
-
- void ReleaseSwapBuffer(void* swapBuffer);
- // Releases a buffer returned by GetSwapBuffer
-
- void SwapElements(void* element1, void* element2, void* swapBuffer);
- // Swaps two elements using the supplied swapbuffer
-
- void QuickSort(ArrayIndex beginIndex,
- ArrayIndex endIndex,
- CompareElementsType CompareItems,
- void* yourDataPtr,
- void* swapBuffer);
- // Uses QuickSort Algorithm, adapted from:
- // Cormen, Leiserson, and Rivest, _Introductions_To_Algorithms_, pp 153-167.
-
- ArrayIndex QSPartition(ArrayIndex beginIndex,
- ArrayIndex endIndex,
- CompareElementsType CompareItems,
- void* yourDataPtr,
- void* swapBuffer);
- // Called by RandomPartition. Partitions the array A[beginIndex..endIndex] in place
-
- ArrayIndex QSRandomPartition(ArrayIndex beginIndex,
- ArrayIndex endIndex,
- CompareElementsType CompareItems,
- void* yourDataPtr,
- void* swapBuffer);
- // Called by QuickSort. Partitions the array A[beginIndex..endIndex] in place
-
- //------------------------------------------------------------------------------------
- // data members
- //------------------------------------------------------------------------------------
-
- public:
- Ptr fArraySpace; // space for actually storing the elements
- // of this TDynamicArray.
-
- CArrayIterator* fIteratorPtr; // pointer to the Iterators in use
-
- ArrayIndex fSize; // number of elements ACTUALLY in the
- // array, from 0 to the limit of memory
-
- ArrayIndex fAllocationIncrement; // Number of elements by which to increase
- // of decrease the allocated size of the
- // array when it needs to grow or shrink.
- // Thus reducing memory manager
- // aggravation.
-
- ArrayIndex fAllocatedSize; // Number of elements for which storage is
- // ALLOCATED
-
- unsigned short fElementSize; // Size in bytes of an element. MUST be a
- // power of 2 ie. 1, 2, 4, 8, 16, etc.
-
- unsigned short fElementSizeShift; // the power of 2 for the element size.
- // Will be used to avoid DIV and MUL
-
- Boolean fFreeRequested; // True if the Free method was called but
- // couldn't be honored because enumeration
- // was in process. Checked at end of
- // enumeration and Free is called if true
- };
-
-
- //----------------------------------------------------------------------------------------
- // TSortedDynamicArray: A TDynamicArray with ordering.
- //----------------------------------------------------------------------------------------
-
- class TSortedDynamicArray : public TDynamicArray
- {
- MA_DECLARE_CLASS;
-
- public:
-
- inline TSortedDynamicArray()
- { }
- // Empty constructor
-
- virtual ~TSortedDynamicArray();
- // Destructor
-
- inline void ISortedDynamicArray(ArrayIndex initialSize, short elementSize)
- { IDynamicArray(initialSize, elementSize); }
- // Initialize a new array with initialSize elements, Always call it once before
- // calling any other method. Never call it twice.
-
- virtual void InsertElementInOrder(void* ElementPtr);
- // Insert an element in its correct position according to the ordering defined by
- // the Compare method
-
- virtual Boolean DoSearchElement(CompareIndexType TestItem,
- void* yourDataPtr,
- ArrayIndex& index);
- // !!! Adapted from TSortedList. Performs a binary search for an element for which
- // TestItem returns true. "index" returns the position of a found item, or where
- // it should be inserted if not found
-
- };
-
-
- #endif
-